<?php
namespace app\admin\controller;

use app\BaseController;
use app\model\User;
use app\model\Token;
use app\model\Wx;
use app\service\BaiDuService;
use app\service\QiniuService;
use app\service\WxService;
use think\facade\Request;

class Index extends BaseController
{
    public function login()
    {
        $param = Request::post();

        if(!isset($param['phone']) || empty($param['phone'])) fail('请输入账号');
        $phone = trim($param['phone']);

        if(!isset($param['pwd']) || empty($param['pwd'])) fail('请输入账号密码');
        $pwd = trim($param['pwd']);

        $userModel = new User();
        $info = $userModel->getUserAdminInfoByPhone($phone,'user.id,user.pername,user.phone,user.pwd,user.position,user.orgid,org.orgname');

        if(!$info)  fail('账号不存在');
        $userPwd = $info['pwd'];
        unset($info['pwd']);
        if($userPwd != encode($pwd))    fail('账号密码错误');

        $tokenModel = new Token();
        $rt['token'] = $tokenModel->generateToken($info['id']);

        success($rt);
    }

    /*
     * 登出
     */
    public function logout()
    {
        $tokenModel = new Token();
        $tokenModel->unsetUserToken(config('user.id'));
        success();
    }

    /*
     * 获取七牛token
     */
    public function token()
    {
        $qn = new QiniuService();
        success(['token'=>$qn->getToken()]);
    }

    /*
     * 获取小程序码
     */
    public function appQrCode()
    {
        $wxService = new WxService();
        $wxModel = new Wx();
        $wxaInfo = $wxModel->getAccessToken();
        if($wxaInfo && $wxaInfo['wt_qrcode'])   success($wxaInfo['wt_qrcode']);

        $rt = $wxService->createQRCode('pages/index/index');
        if($rt){

            $wxModel->updateAccessToken('',['wt_qrcode'=>fileFullPath($rt)]);
            success(fileFullPath($rt));
        }
        fail('二维码获取失败');
    }


}
